/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.ColorDao;
import com.taobao.snowsnow.hibernate.pojo.Color;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class ColorDaoImpl extends BaseDaoImpl implements ColorDao {
	
	public Color findById(int id) throws Exception {
		return (Color) getHibernateTemplate().get(Color.class, id);
	}
	
	public List<Color> findByName(String name) throws Exception {
		return super.findByProperty(Color.class, "name", name);
	}

	public List<Color> findAll() throws Exception {
		return getHibernateTemplate().find("from Color");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Color.class);
	}

	public List<Color> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Color.class, page, limit);
	}

	@Override
	public Color getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Color as o where o.id = (select min(id) from Color)");
		List<Color> list = query.list();
		if(list.size() !=0)
			return (Color) list.get(0);
		else return null;
	}

	@Override
	public Color getLastObject() throws Exception {
		Query query = getSession().createQuery("from Color as o where o.id = (select max(id) from Color)");
		List<Color> list = query.list();
		if(list.size() !=0)
			return (Color) list.get(0);
		else return null;
	}

	@Override
	public Color findByNamePrecise(String name) throws Exception {
		List<Color> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}
}
